package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.service.impl.SetmealServiceImpl;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "套餐模块")
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealServiceImpl setmealService;
    @PostMapping
    @ApiOperation("新增套餐")
    //@CachePut(cacheNames = "SetmealShowBycategoryId_",key = "#dto.id")
    @CacheEvict(cacheNames = "SetmealShowBycategoryId_",allEntries = true)
    public Result add(@RequestBody SetmealDTO dto){
        log.info("新增套餐：传进来的dto是{}",dto);
        setmealService.add(dto);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("条件分页查询")
    public Result getBypage( SetmealPageQueryDTO dto){
        log.info("分页查询的dto：传进来的dto是{}",dto);
        PageResult result = setmealService.getBypage(dto);
        return  Result.success(result);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售，停售")
    @CacheEvict(cacheNames = "SetmealShowBycategoryId_",allEntries = true)
    public Result start_or_stop(@PathVariable String status,String id){
        log.info("套餐起售，停售，传进来的status：{}，id：{}",status,id);
        setmealService.start_or_stop(status,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result getmealByid(@PathVariable String id){
        SetmealVO result = setmealService.getmealByid(id);
        return Result.success(result);
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "SetmealShowBycategoryId_",allEntries = true)
    public Result update(@RequestBody SetmealDTO dto){
        setmealService.update(dto);
        return Result.success();
    }

    @DeleteMapping()
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "SetmealShowBycategoryId_",allEntries = true)
    public Result delete_setmeals(String ids){
        setmealService.delete_setmeals(ids);
        return Result.success();
    }

}
